package com.meepo.framework.design.imageview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;

import com.meepo.framework.R;

public class SlickImageView extends AppCompatImageView {

    private float width, height;

    private final int mLeftTopCornerRadius;
    private final int mLeftBottomCornerRadius;
    private final int mRightTopCornerRadius;
    private final int mRightBottomCornerRadius;
    private final float mWidthHeightRate;

    public SlickImageView(@NonNull Context context) {
        this(context, null);
    }

    public SlickImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlickImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SlickImageView, defStyleAttr, 0);

        mLeftTopCornerRadius = typedArray.getDimensionPixelSize(R.styleable.SlickImageView_siv_leftTop_CornerRadius, 0);
        mLeftBottomCornerRadius = typedArray.getDimensionPixelSize(R.styleable.SlickImageView_siv_leftBottom_CornerRadius, 0);
        mRightTopCornerRadius = typedArray.getDimensionPixelSize(R.styleable.SlickImageView_siv_rightTop_CornerRadius, 0);
        mRightBottomCornerRadius = typedArray.getDimensionPixelSize(R.styleable.SlickImageView_siv_rightBottom_CornerRadius, 0);
        mWidthHeightRate = typedArray.getFloat(R.styleable.SlickImageView_siv_width_height_rate, 0f);

        typedArray.recycle();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);


        width = getWidth();
        height = getHeight();

        if (mWidthHeightRate > 0) {
            height = (int) (width / mWidthHeightRate);
        }

    }


    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        //四个圆角
        path.moveTo(mLeftTopCornerRadius, 0);

        path.lineTo(width - mRightTopCornerRadius, 0);

        path.quadTo(width, 0, width, mRightTopCornerRadius);

        path.lineTo(width, height - mRightBottomCornerRadius);
        path.quadTo(width, height, width - mRightBottomCornerRadius, height);

        path.lineTo(mLeftBottomCornerRadius, height);
        path.quadTo(0, height, 0, height - mLeftBottomCornerRadius);
        path.lineTo(0, mLeftTopCornerRadius);
        path.quadTo(0, 0, mLeftTopCornerRadius, 0);

        canvas.clipPath(path);

        super.onDraw(canvas);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (mWidthHeightRate > 0) {
            heightSize = (int) (widthSize / mWidthHeightRate);
        }

        setMeasuredDimension(widthSize, heightSize);
    }
}
